//
//  CaiPuRenQiContentCollectionViewCell.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CaiPuRenQiContentCollectionViewCell.h"
#import "CaiPuRenQiTableViewCell.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import <MJRefresh.h>
#import "ZJHRenQiModel.h"

@implementation CaiPuRenQiContentCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _tableView = [[UITableView alloc]initWithFrame:self.contentView.frame];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _offset = 0;
        [self.contentView addSubview:_tableView];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _dataArray = [NSMutableArray array];
    [self getData];
    //下拉刷新
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        _offset = 0;
        [self getData];
    }];
    
    //上拉加载
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _offset += 15;
        [self getData];
    }];
    
}


//创建菊花
-(void)createProgressView
{
    self.progressView = [[MBProgressHUD alloc]initWithView:self.tableView];
    [self.contentView addSubview:_progressView];
    _progressView.labelText = @"加载中...";
    _progressView.backgroundColor = [UIColor whiteColor];
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CaiPuRenQiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CaiPuRenQiTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.model = _dataArray[indexPath.row];
    }
#pragma mark cell动画
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190 * ht;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZJHRenQiModel *model = _dataArray[indexPath.row];
    [self.delegate pushCaiPuContentViewWithReceipId:model.RecipeId Title:model.Title];
}
-(void)setTitle:(NSString *)title
{
    _titleString = title;
    [_tableView.header beginRefreshing];
}
-(void)setType:(NSString *)type
{
    _typeString = type;
}
-(void)getData
{
    [self createProgressView];
    NSString *postString = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=c2d17d68154ed97544385f42f056b477&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7CCC2D6423-97F6-437A-B8DE-CBB86A71C6F1%7C608CBAFB-426B-4C02-96CA-F5B029C17D76&format=json&loguid=&method=Recipe.getCollectRecomment&nonce=1446725371&sessionid=1446721866&signmethod=md5&timestamp=1446725371&uuid=f54ccedd4a37e51b4340c5dc18fe353c&v=2&vc=43&vn=v5.4.0";
    NSString *bodyString = [NSString stringWithFormat:@"Title=%@&limit=15&offset=%ld&rid=&sign=&type=%@&uid=&uuid=f54ccedd4a37e51b4340c5dc18fe353c", _titleString, _offset, _typeString];
    [HTTPMethod getDataByString:postString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *allDic = [data objectForKey:@"result"];
        NSArray *listArray = [allDic objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            ZJHRenQiModel *model = [[ZJHRenQiModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_progressView removeFromSuperview];
    }];
}
@end
